package cn.iune.erp.setting;

import java.sql.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.justep.baas.Utils;

import cn.iune.backcenter.author.sysinfo.BillSetting;
import cn.iune.backcenter.author.sysinfo.IBillSettingService;
import cn.iune.backcenter.author.sysinfo.ISysInfoService;
import cn.iune.backcenter.author.sysinfo.SysInfo;

//应用启动时预装载单据类型数据，不再装载在servlet中（）
@Service
public class SettingServiceImpl implements ISettingService {
	
	@Autowired
	private IFileMaxCodeService fileMaxCodeService;
	@Autowired
	private IBillMaxCodeService billMaxCodeService;
	
	@Autowired
	private IBillSettingService billSettingService;
	@Autowired
	private ISysInfoService sysInfoService;
	@Autowired
	private IBillSettingOrgService billSettingOrgService;
	

	
	//获取系统路径和系统名称（通过ID）
	
	@Override
	public JSONObject getSysInfoUrlByID(String id) {
		SysInfo sysInfo = sysInfoService.getByID(id);
		/*
		List<SysInfo> list = sysInfoCache.getPagList();
		for(SysInfo obj: list) {
			if(obj.getfID().equals(id)) {
				sysInfo = obj;
			}
		}
		*/
		
		JSONObject json = new JSONObject();
		if(sysInfo==null){
			json.put("code", -1);
			json.put("msg", "不存在");
		}else{
			json.put("code", 0);
			json.put("msg", "success");
			json.put("url", sysInfo.getAbsoluteUrl());
			json.put("name", sysInfo.getSystemName());
		}
		return json;
	}
	
	
	//已经合二为一啦。
	/*
	//档案流水号
	public String getFileMaxCode(String billType, String enterpriseNo) {
		
		String preChar = null;

		JSONArray billtypes = this.getBillTypes();
		//System.out.println("billtypes-->>"+billtypes.size());
		
		for(int i=0;i<billtypes.size();i++) {
			JSONObject obj = billtypes.getJSONObject(i);
			//System.out.println(i+"-->>obj-->>"+obj);
			if(obj.getString("billType").equals(billType)) {
				//找到匹配的行了
				//System.out.println("billtypeName-->>"+obj.toJSONString());
				preChar = obj.getString("preChar");
				break;
			}
		}
		
		if(preChar == null) {
			return null;
		}

//		long now = System.currentTimeMillis();
//		return preChar + String.valueOf(now);
		
		String num = fileMaxCodeService.generate(billType, enterpriseNo);
		String billNo = preChar + num;
		
		return billNo;
	}
	*/
	
	//单据流水号（通用）
	@Transactional
	@Override
	public String getMaxCode(String billType, String enterpriseNo) {
		
		String preChar = null;
		String codeType = null;
		
		BillSetting billSetting = billSettingService.getByNo(billType);
		/*
		List<BillSetting> list = sysInfoCache.getBillSettingList();
		for(BillSetting obj: list) {
			if(obj.getBillType().equals(billType)) {
				billSetting = obj;
			}
		}
		*/

		if(billSetting!=null) {
			codeType = billSetting.getCodeType();
			preChar = billSetting.getPreChar();
		}

		if(preChar == null) {
			return null;
		}

//		long now = System.currentTimeMillis();
//		return preChar + String.valueOf(now);
		
//		Date orderDate = Utils.getCurrentDate();
//		String num = billMaxCodeService.generate(orderDate, billType, enterpriseNo);
		
		String num = "-编码类型错误";
		if("NB".equals(codeType)) {
			//流水号
			num = fileMaxCodeService.generate(billType, enterpriseNo);
		}
		if("DT".equals(codeType)) {
			//日期型
			Date orderDate = Utils.getCurrentDate();
			num = billMaxCodeService.generate(orderDate, billType, enterpriseNo);
		}
		
		
		String billNo = preChar + num;
		
		return billNo;
	}


	//获取单据初始化参数
	@Override
	public Map<String,Object> getBillParam(String billType,String enterpriseNo){
		
		System.out.println("系统信息-->billType:"+billType);
		System.out.println("系统信息-->enterpriseNo:"+enterpriseNo);
		Map<String,Object> map =  new HashMap<String, Object>();
		
		String state = "success";
		String fMasterID="";
		String billTypeName="";
		int flowType = 0;
		String revOpay="";
		int direction = 0;
		String costingCode="";
		String autoCode="";
		String origin="";
		String originName="";
		String billNo = "";
		String billLog = "";
		int dealType = 0;
		
		String periodStatus="N";
		String periodNo="";
		

//		state = "failure";
		System.out.println("系统提示-->单据信息未单独配置，使用系统默认配置。");
		BillSetting billSetting = billSettingService.getByNo(billType);
		/*
		List<BillSetting> list = sysInfoCache.getBillSettingList();
		for(BillSetting obj: list) {
			if(obj.getBillType().equals(billType)) {
				billSetting = obj;
			}
		}
		*/
		
		if(billSetting!=null) {
			fMasterID = billSetting.getfMasterID();
			
			billTypeName = billSetting.getBillTypeName();
			flowType = billSetting.getFlowType();
			revOpay = billSetting.getRevOpay();
			direction = billSetting.getDirection();//单据类型（红字蓝字。1和-1）--必须
			costingCode = billSetting.getCostingCode();//影响成本码（S和V）--必须
			autoCode = billSetting.getAutoCode();//自动编码（Y和N）--必须
			origin = billSetting.getOrigin();//默认数据来源--必须
			originName = billSetting.getOriginName();
			//自动产生编码
			if(autoCode.equals("Y")){
				String preChar = billSetting.getPreChar();
				String codeType = billSetting.getCodeType();
				String num = "-编码类型错误";
				if("NB".equals(codeType)) {
					//流水号
					num = fileMaxCodeService.generate(billType, enterpriseNo);
				}
				if("DT".equals(codeType)) {
					//日期型
					Date orderDate = Utils.getCurrentDate();
					num = billMaxCodeService.generate(orderDate, billType, enterpriseNo);
				}
				billNo = preChar + num;
			}
			
			billLog = billSetting.getBillLog();
			dealType = billSetting.getDealType();
				
		}else {
			System.out.println("系统提示-->系统默认配置不存在，请联系系统管理员。");
		}
		
		
		map.put("state", state);
		
		map.put("fMasterID", fMasterID);
		map.put("billType", billType);
		map.put("billTypeName", billTypeName);
		map.put("flowType", flowType);
		map.put("revOpay", revOpay);
		map.put("direction", direction);
		map.put("costingCode", costingCode);
		map.put("autoCode", autoCode);
		map.put("origin", origin);
		map.put("originName", originName);
		map.put("billNo", billNo);
		map.put("billLog", billLog);
		map.put("dealType", dealType);
		
//		map.put("storeObject", storeObject);
//		map.put("storageType", storageType);
//		map.put("storageTypeD", storageTypeD);
//		map.put("dealObject", dealObject);
//		map.put("invoiceCategory", invoiceCategory);
//		map.put("invoiceCategoryLabel", invoiceCategoryLabel);
		
		map.put("periodStatus", periodStatus);
		map.put("periodNo", periodNo);

		return map;
	}

	//复制数据。从BillSetting到BillSettingOrg
	@Transactional
	@Override
	public JSONObject copyBillSetting(String billType, String userId, String enterpriseNo, String enterpriseName) {
		JSONObject json = new JSONObject();
		BillSetting billSetting = billSettingService.getByNo(billType);
		if(billSetting==null) {
			json.put("code",-1);
			json.put("msg", "failure");
			return json;
		}
		
		//获取数据
		String billTypeName = billSetting.getBillTypeName();
		String appId = billSetting.getAppId();
		String appName = billSetting.getAppName();
		String autoCode = billSetting.getAutoCode();
		String autoCodeLabel = billSetting.getAutoCodeLabel();
		String billLog = billSetting.getBillLog();
		int codeCount = billSetting.getCodeCount();
		String codeType = billSetting.getCodeType();
		String codeTypeLabel = billSetting.getCodeTypeLabel();
		String costingCode = billSetting.getCostingCode();
		String costingCodeLabel = billSetting.getCostingCodeLabel();
		int direction = billSetting.getDirection();
		String directionLabel = billSetting.getDirectionLabel();
		int flowType = billSetting.getFlowType();
		String flowTypeLabel = billSetting.getFlowTypeLabel();
		String origin = billSetting.getOrigin();
		String originName = billSetting.getOriginName();
		String preChar = billSetting.getPreChar();
		String revOpay = billSetting.getRevOpay();
		String revOpayLabel = billSetting.getRevOpayLabel();
		String fMasterID = billSetting.getfMasterID();
		
		
		BillSettingOrg billSettingOrg = billSettingOrgService.getByNo(billType, enterpriseNo);
		if(billSettingOrg==null) {
			//新增
			billSettingOrg = new BillSettingOrg();
			billSettingOrg.setfID(Utils.getUUID());
			
			billSettingOrg.setBillType(billType);
			
			billSettingOrg.setEnterpriseNo(enterpriseNo);
			billSettingOrg.setEnterpriseName(enterpriseName);
			billSettingOrg.setVersion(0);
			
			
			//复制数据
			billSettingOrg.setBillTypeName(billTypeName);
			billSettingOrg.setAppId(appId);
			billSettingOrg.setAppName(appName);
			billSettingOrg.setAutoCode(autoCode);
			billSettingOrg.setAutoCodeLabel(autoCodeLabel);
			billSettingOrg.setBillLog(billLog);
			billSettingOrg.setCodeCount(codeCount);
			billSettingOrg.setCodeType(codeType);
			billSettingOrg.setCodeTypeLabel(codeTypeLabel);
			billSettingOrg.setCostingCode(costingCode);
			billSettingOrg.setCostingCodeLabel(costingCodeLabel);
			billSettingOrg.setDirection(direction);
			billSettingOrg.setDirectionLabel(directionLabel);
			billSettingOrg.setFlowType(flowType);
			billSettingOrg.setFlowTypeLabel(flowTypeLabel);
			billSettingOrg.setOrigin(origin);
			billSettingOrg.setOriginName(originName);
			billSettingOrg.setPreChar(preChar);
			billSettingOrg.setRevOpay(revOpay);
			billSettingOrg.setRevOpayLabel(revOpayLabel);
			billSettingOrg.setfMasterID(fMasterID);
			billSettingOrg.setUserId(userId);

			billSettingOrgService.insert(billSettingOrg);
		}else {
			
			//复制数据
			billSettingOrg.setBillTypeName(billTypeName);
			billSettingOrg.setAppId(appId);
			billSettingOrg.setAppName(appName);
			billSettingOrg.setAutoCode(autoCode);
			billSettingOrg.setAutoCodeLabel(autoCodeLabel);
			billSettingOrg.setBillLog(billLog);
			billSettingOrg.setCodeCount(codeCount);
			billSettingOrg.setCodeType(codeType);
			billSettingOrg.setCodeTypeLabel(codeTypeLabel);
			billSettingOrg.setCostingCode(costingCode);
			billSettingOrg.setCostingCodeLabel(costingCodeLabel);
			billSettingOrg.setDirection(direction);
			billSettingOrg.setDirectionLabel(directionLabel);
			billSettingOrg.setFlowType(flowType);
			billSettingOrg.setFlowTypeLabel(flowTypeLabel);
			billSettingOrg.setOrigin(origin);
			billSettingOrg.setOriginName(originName);
			billSettingOrg.setPreChar(preChar);
			billSettingOrg.setRevOpay(revOpay);
			billSettingOrg.setRevOpayLabel(revOpayLabel);
			billSettingOrg.setfMasterID(fMasterID);
			billSettingOrg.setUserId(userId);

			billSettingOrgService.update(billSettingOrg);
		}
		
		
		json.put("code",0);
		json.put("msg", "success");
		return json;
	}
	
	
}
